package cn.lyf.stream;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class AnalyzeMap implements Function<Map<String, Object>, Map<String, Object>> {
    //
    Function<String, Function> router;
    // data ---maps---> [data]
    ApplyMap<Object, Map.Entry<String, Object>> resolvers;

    @Override
    public Map<String, Object> apply(Map<String, Object> json) {
        return json.entrySet().stream().map(
                kv -> {
                    Function fx = router.apply(kv.getKey());
                    Object data=kv.getValue();
                    if (fx instanceof AnalyzeMap) {
                        Map<String, Object> map = ((AnalyzeMap) fx).resolvers.apply(data).collect(
                                Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)
                        );
                        kv.setValue(fx.apply(map));
                        return kv;
                    }else {
                        kv.setValue(fx.apply(data));
                        return kv;
                    }
                }
        ).collect(Collectors.toMap(
                Map.Entry::getKey, Map.Entry::getValue
        ));
    }

}
